// key.c
//

#include <avr/io.h>
#include "../mcu/types.h"
#include "../mcu/delay.h"
#include "key.h"


u_char key_check_press(void)
{
	if( ! (PINB & (1 << KEY_1)) )
	{
		wait_1ms(1);

		if( ! (PINB & (1 << KEY_1)) )
			return KEY_1;
	}

	if( ! (PINB & (1 << KEY_2)) )
	{
		wait_1ms(1);

		if( ! (PINB & (1 << KEY_2)) )
			return KEY_2;
	}

	if( ! (PINB & (1 << KEY_3)) )
	{
		wait_1ms(1);

		if( ! (PINB & (1 << KEY_3)) )
			return KEY_3;
	}

	if( ! (PINB & (1 << KEY_4)) )
	{
		wait_1ms(1);

		if( ! (PINB & (1 << KEY_4)) )
			return KEY_4;
	}

	return KEY_NONE;
}

bool key_check_release(u_char key)
{
	if( ! (PINB & (1 << key)) )
		return false;

	return true;
}

void key_wait_release(u_char key, void (* callback)(void))
{
	while( ! (PINB & (1 << key)) )
	{
		if( callback )
			callback();
	}
}
